package com.hh.front.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.hh.common.constants.Constants;
import com.hh.common.constants.SysConfigConstants;
import com.hh.common.constants.SysGroupDataConstants;
import com.hh.common.exception.HhException;
import com.hh.common.model.product.StoreProduct;
import com.hh.common.model.system.SystemConfig;
import com.hh.common.page.CommonPage;
import com.hh.common.request.PageParamRequest;
import com.hh.common.response.IndexInfoResponse;
import com.hh.common.response.IndexProductResponse;
import com.hh.common.response.JingangAreaResponse;
import com.hh.common.response.ProductActivityItemResponse;
import com.hh.common.response.pagemanagement.HomeRecommendResponse;
import com.hh.common.utils.HhUtil;
import com.hh.common.vo.MyRecord;
import com.hh.front.service.IndexService;
import com.hh.service.delete.ProductUtils;
import com.hh.service.service.*;
import com.hh.service.service.pagemanagement.BannerService;
import com.hh.service.service.pagemanagement.JingangAreaService;
import com.hh.service.service.pagemanagement.ProductRecommendService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* IndexServiceImpl 接口实现
*  +----------------------------------------------------------------------
 *  | hh [ hh赋能开发者，助力企业发展 ]
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2016~2022 https://www.hh.com All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Licensed hh并不是自由软件，未经许可不能去掉hh相关版权
 *  +----------------------------------------------------------------------
 *  | Author: hh Team <admin@hh.com>
 *  +----------------------------------------------------------------------
*/
@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private SystemGroupDataService systemGroupDataService;

    @Autowired
    private SystemConfigService systemConfigService;


    @Autowired
    private StoreProductService storeProductService;

    @Autowired
    private ProductUtils productUtils;


    @Autowired
    private BannerService bannerService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductRecommendService productRecommendService;

    @Autowired
    private JingangAreaService jingangAreaService;

    @Autowired
    private UserService userService;

    /**
     * 首页数据
     * banner、金刚区、广告位
     */
    @Override
    public IndexInfoResponse getIndexInfo() {
        IndexInfoResponse indexInfoResponse = new IndexInfoResponse();
        //首页banner滚动图
        indexInfoResponse.setBanner(bannerService.getList(1,5));
        //首页商品分类
        indexInfoResponse.setCategory(categoryService.getCategoryOneLevel());
        Map<String, HomeRecommendResponse> recommend = productRecommendService.getRecommend();
        indexInfoResponse.setRecommends(recommend);
        return indexInfoResponse;
    }

    /**
     * 热门搜索
     * @return List<HashMap<String, String>>
     */
    @Override
    public List<HashMap<String, Object>> hotKeywords() {
        return systemGroupDataService.getListMapByGid(SysGroupDataConstants.GROUP_DATA_ID_INDEX_KEYWORDS);
    }

    /**
     * 微信分享配置
     * @return Object
     */
    @Override
    public HashMap<String, String> getShareConfig() {
        HashMap<String, String> map = new HashMap<>();
        HashMap<String, String> info = systemConfigService.info(Constants.CONFIG_FORM_ID_PUBLIC,false);
        if(info == null) {
            throw new HhException("请配置公众号分享信息！");
        }
        map.put("img", info.get(SysConfigConstants.CONFIG_KEY_ADMIN_WECHAT_SHARE_IMAGE));
        map.put("title", info.get(SysConfigConstants.CONFIG_KEY_ADMIN_WECHAT_SHARE_TITLE));
        map.put("synopsis", info.get(SysConfigConstants.CONFIG_KEY_ADMIN_WECHAT_SHARE_SYNOSIS));
        return map;
    }

    /**
     * 获取首页商品列表
     * @param type 类型 【1 精品推荐 2 热门榜单 3首发新品 4促销单品】
     * @param pageParamRequest 分页参数
     * @return List
     */
    @Override
    public CommonPage<IndexProductResponse> findIndexProductList(Integer type, PageParamRequest pageParamRequest) {
        if (type < Constants.INDEX_RECOMMEND_BANNER || type > Constants.INDEX_BENEFIT_BANNER) {
            return CommonPage.restPage(new ArrayList<>());
        }
        List<StoreProduct> storeProductList = storeProductService.getIndexProduct(type, pageParamRequest);
        if(CollUtil.isEmpty(storeProductList)) {
            return CommonPage.restPage(new ArrayList<>());
        }
        CommonPage<StoreProduct> storeProductCommonPage = CommonPage.restPage(storeProductList);

        List<IndexProductResponse> productResponseArrayList = new ArrayList<>();
        for (StoreProduct storeProduct : storeProductList) {
            IndexProductResponse productResponse = new IndexProductResponse();
            List<Integer> activityList = HhUtil.stringToArrayInt(storeProduct.getActivity());
            // 活动类型默认：直接跳过
            if (activityList.get(0).equals(Constants.PRODUCT_TYPE_NORMAL)) {
                BeanUtils.copyProperties(storeProduct, productResponse);
                productResponseArrayList.add(productResponse);
                continue;
            }
            // 根据参与活动添加对应商品活动标示
            HashMap<Integer, ProductActivityItemResponse> activityByProduct =
                    productUtils.getActivityByProduct(storeProduct.getId(), storeProduct.getActivity());
            if (CollUtil.isNotEmpty(activityByProduct)) {
                for (Integer activity : activityList) {
                    if (activity.equals(Constants.PRODUCT_TYPE_NORMAL)) {
                        break;
                    }

                }
            }
            BeanUtils.copyProperties(storeProduct, productResponse);
            productResponseArrayList.add(productResponse);
        }
        CommonPage<IndexProductResponse> productResponseCommonPage = CommonPage.restPage(productResponseArrayList);
        BeanUtils.copyProperties(storeProductCommonPage, productResponseCommonPage, "list");
        return productResponseCommonPage;
    }

    /**
     * 获取颜色配置
     * @return SystemConfig
     */
    @Override
    public SystemConfig getColorConfig() {
        return systemConfigService.getColorConfig();
    }

    /**
     * 获取版本信息
     * @return MyRecord
     */
    @Override
    public MyRecord getVersion() {
        MyRecord record = new MyRecord();
        // app版本号
        record.set("appVersion", systemConfigService.getValueByKey(Constants.CONFIG_APP_VERSION,false, Constants.PLATFORM_MER_ID));
        record.set("androidAddress", systemConfigService.getValueByKey(Constants.CONFIG_APP_ANDROID_ADDRESS,false, Constants.PLATFORM_MER_ID));
        record.set("iosAddress", systemConfigService.getValueByKey(Constants.CONFIG_APP_IOS_ADDRESS,false, Constants.PLATFORM_MER_ID));
        record.set("openUpgrade", systemConfigService.getValueByKey(Constants.CONFIG_APP_OPEN_UPGRADE,false, Constants.PLATFORM_MER_ID));
        return record;
    }

    /**
     * 获取全局本地图片域名
     * @return String
     */
    @Override
    public String getImageDomain() {
        String localUploadUrl = systemConfigService.getValueByKey("localUploadUrl",false, Constants.PLATFORM_MER_ID);
        return StrUtil.isBlank(localUploadUrl) ? "" : localUploadUrl;
    }

    @Override
    public List<JingangAreaResponse> getJingangArea() {
        return jingangAreaService.getFrontJingangAreaList();
    }
}

